<template>
  <el-cascader
    :value="value"
    size="mini"
    :options="treeData"
    :props="props"
    separator="-"
    @change="idChange"
  />
</template>
<script>
import { transListToTreeData } from '@/utils'
import { getDepartment } from '@/api/department'
export default {
  props: {
    value: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      treeData: [],
      props: {
        value: 'id', // 要储存的字段
        label: 'name' // 要展示的字段
      }
    }
  },
  created() {
    this.getDepartmentFn()
  },
  methods: {
    async getDepartmentFn() {
      //   const res = await getDepartment();
      //   const ss = transListToTreeData(res, 0);
      //   console.log(ss);
      //   简写
      this.treeData = transListToTreeData(await getDepartment(), 0)
    },
    // 选中的部门数据
    idChange(list) {
      if (list.length > 0) {
        this.$emit('input', list[list.length - 1])
      } else {
        this.$emit('input', null)
      }
    }
  }
}
</script>
<style scoped></style>
